package dsf.eshop.user.shiro;

import dsf.eshop.user.dao.RoleMapper;
import dsf.eshop.user.dao.UserMapper;
import dsf.eshop.user.dao.UserRoleMapper;
import dsf.eshop.user.entity.User;
import dsf.eshop.user.entity.UserExample;
import dsf.eshop.user.entity.UserRole;
import dsf.eshop.user.entity.UserRoleExample;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static javax.swing.UIManager.get;

@Slf4j
@Component
public class MyShiroRealm extends AuthorizingRealm {

    @Resource
    UserMapper userMapper;

    @Resource
    UserRoleMapper userRoleMapper;

    @Resource
    private RoleMapper roleMapper;

    @Override
    public String getName() {
        return "MyReslm";
    }

    @Override
    public boolean supports(AuthenticationToken authenticationToken) {
        return true;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) {

        String username = (String) authenticationToken.getPrincipal();
        log.info("########当前登录 " + username + "########");
        if(username == null) {
            return null;
        }
        UserExample userExample = new UserExample();
        userExample.createCriteria().andEmailEqualTo(username);
        List<User> result = userMapper.selectByExample(userExample);
        if(null == result || result.size() == 0) {
            return null;
        }
        User user = result.get(0);
        AuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(user.getEmail(), user.getPassword(), getName());
        return authenticationInfo;
    }

    @Override
    @Transactional
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {

        log.info("##################执行Shiro权限认证##################");

        String email = (String) super.getAvailablePrincipal(principalCollection);
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andEmailEqualTo(email);
        if(null == userMapper.selectByExample(userExample) || userMapper.selectByExample(userExample).size() == 0) {
            return null;
        }
        User user = userMapper.selectByExample(userExample).get(0);
        UserRoleExample userRoleExample = new UserRoleExample();
        userRoleExample.createCriteria().andUserIdEqualTo(user.getUserId());
        List<UserRole> userRoles = userRoleMapper.selectByExample(userRoleExample);
        Set<String> rolesName = new HashSet<String>();
        for(UserRole userRole : userRoles){
            rolesName.add(roleMapper.selectByPrimaryKey(userRole.getRoleId()).getRoleName());
        }
        log.info("当前用户角色 >>>>>" + rolesName);
        AuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
        ((SimpleAuthorizationInfo) authorizationInfo).setRoles(rolesName);
        return authorizationInfo;
    }
}
